// LVGL VERSION: 8.2.0

#include "ui.h"
#include "ui_helpers.h"
#include "./screens/Inc/ui_HomePage.h"
#include "../Func/Inc/pubsub.h"

///////////////////// TEST LVGL SETTINGS ////////////////////
//#if LV_COLOR_DEPTH != 16
//    #error "LV_COLOR_DEPTH should be 16bit to match SquareLine Studio's settings"
//#endif
//#if LV_COLOR_16_SWAP !=0
//    #error "LV_COLOR_16_SWAP should be 0 to match SquareLine Studio's settings"
//#endif
lv_group_t *groupRect;
extern Page_t Page_Power;
lv_ft_info_t ttf_info_12;
lv_ft_info_t ttf_info_16;
lv_ft_info_t ttf_info_20;
lv_ft_info_t ttf_info_24;
lv_ft_info_t ttf_info_28;
lv_ft_info_t ttf_info_32;
lv_ft_info_t ttf_info_34;
lv_ft_info_t ttf_info_36;
lv_ft_info_t ttf_info_40;
lv_ft_info_t ttf_info_44;
lv_ft_info_t ttf_info_48;
lv_ft_info_t ttf_info_56;

///////////////////// CallBack functions ////////////////////
void SDL_KeyBoard_Subscriber(PubSub_Message_t message) {
    printf("change screen\r\n");
    Page_Back();
}

/////////////////////// Timer //////////////////////
static void main_timer(lv_timer_t * timer) {
    // do something in main timer
}

#define FONT_PATH                                               "/oem/hir_res/font/AlibabaPuHuiTi-Regular.ttf"
void loading_font(const char *font_path) {
    /*Init freetype library
     *Cache max 64 faces and 1 size*/
    lv_freetype_init(64, 1, 0);

    /*Create a font*/

    xt_prt("%s:%s\n", __func__,font_path);

    ttf_info_12.name = font_path;
    ttf_info_12.weight = 12;
    ttf_info_12.style = FT_FONT_STYLE_NORMAL;
    lv_ft_font_init(&ttf_info_12);

    ttf_info_16.name = font_path;
    ttf_info_16.weight = 16;
    ttf_info_16.style = FT_FONT_STYLE_NORMAL;
    lv_ft_font_init(&ttf_info_16);


    ttf_info_20.name = font_path;
    ttf_info_20.weight = 20;
    ttf_info_20.style = FT_FONT_STYLE_NORMAL;
    lv_ft_font_init(&ttf_info_20);

    ttf_info_24.name = font_path;
    ttf_info_24.weight = 24;
    ttf_info_24.style = FT_FONT_STYLE_NORMAL;
    lv_ft_font_init(&ttf_info_24);

    ttf_info_28.name = font_path;
    ttf_info_28.weight = 28;
    ttf_info_28.style = FT_FONT_STYLE_NORMAL;
    lv_ft_font_init(&ttf_info_28);

    ttf_info_32.name = font_path;
    ttf_info_32.weight = 32;
    ttf_info_32.style = FT_FONT_STYLE_NORMAL;
    lv_ft_font_init(&ttf_info_32);

    ttf_info_34.name = font_path;
    ttf_info_34.weight = 34;
    ttf_info_34.style = FT_FONT_STYLE_NORMAL;
    lv_ft_font_init(&ttf_info_34);

    ttf_info_36.name = font_path;
    ttf_info_36.weight = 36;
    ttf_info_36.style = FT_FONT_STYLE_NORMAL;
    lv_ft_font_init(&ttf_info_36);

    ttf_info_40.name = font_path;
    ttf_info_40.weight = 40;
    ttf_info_40.style = FT_FONT_STYLE_NORMAL;
    lv_ft_font_init(&ttf_info_40);

    ttf_info_44.name = font_path;
    ttf_info_44.weight = 44;
    ttf_info_40.style = FT_FONT_STYLE_NORMAL;
    lv_ft_font_init(&ttf_info_44);

    ttf_info_48.name = font_path;
    ttf_info_48.weight = 48;
    ttf_info_48.style = FT_FONT_STYLE_NORMAL;
    lv_ft_font_init(&ttf_info_48);

    ttf_info_56.name = font_path;
    ttf_info_56.weight = 56;
    ttf_info_56.style = FT_FONT_STYLE_NORMAL;
    lv_ft_font_init(&ttf_info_56);
}

/////////////////////// ui_initialize //////////////////////
void ui_init(void) {
    char *p_buf = FONT_PATH;
    loading_font(p_buf);
    lv_disp_t * dispp = lv_disp_get_default();
    lv_theme_t * theme = lv_theme_default_init(dispp, lv_palette_main(LV_PALETTE_BLUE), lv_palette_main(LV_PALETTE_RED),
                                               true, LV_FONT_DEFAULT);
    lv_disp_set_theme(dispp, theme);
    Pages_init();
    //timer
    lv_timer_t * ui_MainTimer = lv_timer_create(main_timer, 1000,  NULL);
}

int XtKeyPad2HomePMapping(int val){
    int i_ret = val;
    switch(val){
        case LV_KEY_RIGHT:
        case LV_KEY_MENU:{
            i_ret = XT_KEY_EVENT_DOWN;
        }break;
        case LV_KEY_RIGHT_LONG:
        case LV_KEY_MENU_LONG:{
            i_ret = XT_KEY_EVENT_MENU;
        }break;
        case LV_KEY_POWER:
        case LV_KEY_CAMERA:{
            i_ret = XT_KEY_EVENT_ENTER;
        }break;
        case LV_KEY_POWER_LONG:
        case LV_KEY_CAMERA_LONG:{
            i_ret = XT_KEY_EVENT_EXIST;
        }break;
        default:{
        }break;
    }
    xt_prt("val:%d i_ret:%d\n", val, i_ret);
    return i_ret;
}
void ui_event_BackPage(lv_event_t * e) {
   lv_event_code_t event_code = lv_event_get_code(e);
    lv_obj_t * target = lv_event_get_target(e);
    if(event_code == LV_EVENT_GESTURE) {
        if(lv_indev_get_gesture_dir(lv_indev_get_act()) == LV_DIR_LEFT) {
            Page_Back();
        }
    }
    else if(event_code == LV_EVENT_KEY) {
        int m_key = XtKeyPad2HomePMapping(lv_event_get_key(e));
        xt_prt("m_key:%d\n", m_key);
        switch(m_key) {
            case XT_KEY_EVENT_MENU:{
                Page_Load(&Page_Power);
            }break;
            case XT_KEY_EVENT_DOWN:{
                lv_group_focus_next(groupRect);
            }break;
            case XT_KEY_EVENT_EXIST:{
                Page_Back();
            }break;
            case XT_KEY_EVENT_ENTER:{
                if(e->user_data != NULL) {
                    Page_t *page = (Page_t*) (e->user_data);
                    Page_Load(page);
                }
                else {
                    ui_mbox_create("开发中");
                }
            }break;
        }
    }
}

void keypad_switch_next_btn(void) {
    static lv_obj_t *rect1, *rect2, *rect3; //全局变量
    groupRect = lv_group_create();
    lv_indev_set_group(indev_keypad, groupRect);
    lv_obj_t * obj = lv_obj_create(lv_scr_act());
    lv_obj_set_size(obj, LV_HOR_RES, LV_VER_RES);
    
    rect1 = lv_btn_create(obj);
    lv_obj_set_size(rect1, LV_PCT(20), LV_PCT(20));
    lv_obj_align(rect1, LV_ALIGN_TOP_LEFT, 10, 20);
    lv_obj_add_event_cb(rect1, ui_event_BackPage, LV_EVENT_ALL, (void *)NULL);
 
    rect2 = lv_btn_create(obj);
    lv_obj_set_size(rect2, LV_PCT(20), LV_PCT(20));
    lv_obj_align(rect2, LV_ALIGN_TOP_LEFT, 80, 20);
    lv_obj_add_event_cb(rect2, ui_event_BackPage, LV_EVENT_KEY, (void *)NULL);
 
    rect3 = lv_btn_create(obj);
    lv_obj_set_size(rect3, LV_PCT(20), LV_PCT(20));
    lv_obj_align(rect3, LV_ALIGN_TOP_LEFT, 170, 20);
    lv_obj_add_event_cb(rect3, ui_event_BackPage, LV_EVENT_KEY, (void *)NULL);
 
 
    lv_group_add_obj(groupRect, rect1);
    lv_group_add_obj(groupRect, rect2);
    lv_group_add_obj(groupRect, rect3);

}

